package com.webwork.mood.system.dao.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.webwork.mood.common.dao.impl.SimpleDao;
import com.webwork.mood.system.dao.UserDao;
import com.webwork.mood.system.dataobject.User;
import com.webwork.mood.util.ColumnLimit;
import com.webwork.mood.util.DBSimpleSQL;
import com.webwork.mood.util.StrManager;

@Repository
public class UserDaoImpl extends SimpleDao implements UserDao {
	
	public UserDaoImpl() {
		super();
		this.dbTable = "t_user";
		dbColumns.clear();
		dbColumns.put("userId", "USER_ID");
		dbColumns.put("userName", "USER_NAME");
		dbColumns.put("password", "PASSWORD");
		dbColumns.put("telephone", "TELEPHONE");
		dbColumns.put("weixinId", "WEIXIN_ID");
		dbColumns.put("jobId", "JOB_ID");
		dbColumns.put("branchId", "BRANCH_ID");
		dbColumns.put("isManager", "IS_MANAGER");
		dbColumns.put("registDate", "REGIST_DATE");
	}
	
	@Override
	public ColumnLimit condition(Object object) {
		if (!(object instanceof User))
			return new ColumnLimit();
		User o = (User) object;
		ColumnLimit ret = new ColumnLimit();
		if (!StrManager.isEmpty(o.getUserId())) {
			ret.add(dbColumns.get("userId") + " = ");
		}
		if (!StrManager.isEmpty(o.getPassword())) {
			ret.add(dbColumns.get("password") + " = ");
		}
		if (!StrManager.isEmpty(o.getUserName())) {
			ret.add(dbColumns.get("userName") + " like ");
		}
		if (!StrManager.isEmpty(o.getTelephone())) {
			ret.add(dbColumns.get("telephone") + " like ");
		}
		if (!StrManager.isEmpty(o.getWeixinId())) {
			ret.add(dbColumns.get("weixinId") + " = ");
		}
		if (!StrManager.isEmpty(o.getJobId())) {
			ret.add(dbColumns.get("jobId") + " = ");
		}
		if (!StrManager.isEmpty(o.getBranchId())) {
			ret.add(dbColumns.get("branchId") + " = ");
		}
		if (o.getIsManager() != -1) {
			ret.add(dbColumns.get("isManager") + " = ");
		}
		if (o.getRegistDate() != null) {
			ret.add(dbColumns.get("registDate") + " = ");
		}
		return ret;
	}

	@Override
	public Object[] statement(Object object) {
		User o = (User) object;
		List<Object> ret = new ArrayList<Object>();
		if (!StrManager.isEmpty(o.getUserId())) {
			ret.add(o.getUserId());
		}
		if (!StrManager.isEmpty(o.getPassword())) {
			ret.add(o.getPassword());
		}
		if (!StrManager.isEmpty(o.getUserName())) {
			ret.add("%" + o.getUserName() + "%");
		}
		if (!StrManager.isEmpty(o.getTelephone())) {
			ret.add("%" + o.getTelephone() + "%");
		}
		if (!StrManager.isEmpty(o.getWeixinId())) {
			ret.add(o.getWeixinId());
		}
		if (!StrManager.isEmpty(o.getJobId())) {
			ret.add(o.getJobId());
		}
		if (!StrManager.isEmpty(o.getBranchId())) {
			ret.add(o.getBranchId());
		}
		if (o.getIsManager() != -1) {
			ret.add(o.getIsManager());
		}
		if (o.getRegistDate() != null) {
			ret.add(o.getRegistDate());
		}
		return ret.toArray();
	}

	@Override
	public List<Object> pushObject(Object object) {
		User o = (User) object;
		List<Object> objs = new ArrayList<Object>();
		objs.add(o.getUserId());
		objs.add(o.getUserName());
		objs.add(o.getPassword());
		objs.add(o.getTelephone());
		objs.add(o.getWeixinId());
		objs.add(o.getJobId());
		objs.add(o.getBranchId());
		objs.add(o.getIsManager());
		objs.add(o.getRegistDate());
		return objs;
	}

	@Override
	public Object catchObject(Map<String, Object> map) {
		User ret = new User();
		ret.setUserId((String) map.get(dbColumns.get("userId")));
		ret.setUserName((String) map.get(dbColumns.get("userName")));
		ret.setPassword((String) map.get(dbColumns.get("password")));
		ret.setTelephone((String) map.get(dbColumns.get("telephone")));
		ret.setWeixinId((String) map.get(dbColumns.get("weixinId"))); 
		ret.setJobId((String) map.get(dbColumns.get("jobId")));
		ret.setBranchId((String) map.get(dbColumns.get("branchId")));
		ret.setIsManager((Integer) map.get(dbColumns.get("isManager")));
		ret.setRegistDate((Timestamp) map.get(dbColumns.get("registDate")));
		return ret;
	}
	
	@Override
	public void insert(User o) {
		String sql = DBSimpleSQL.insert(dbTable, dbColumns.size());
		this.getJdbcTemplate().update(sql, this.pushObject(o).toArray());
	}

	@Override
	public void update(User o) {
		ColumnLimit limit = new ColumnLimit();
		limit.add(dbColumns.get("userId") + " = ");
		String sql = DBSimpleSQL.update(dbTable, dbColumns, limit);
		List<Object> objs = this.pushObject(o);
		objs.add(o.getUserId());
		this.getJdbcTemplate().update(sql, objs.toArray());
	}

	@Override
	public void delete(String userId) {
		ColumnLimit limit = new ColumnLimit();
		limit.add(dbColumns.get("userId") + " = ");
		String sql = DBSimpleSQL.del(dbTable, limit);
		this.getJdbcTemplate().update(sql, userId);
	}

	@Override
	public List<User> query(User o, int page, int size) {
		List<User> ret = new ArrayList<User>();
		String sql = DBSimpleSQL.selectAll(dbTable, this.condition(o), page, size);
		List<Map<String, Object>> maps = this.getJdbcTemplate().queryForList(sql, this.statement(o));
		for (Map<String, Object> map : maps) {
			ret.add((User) this.catchObject(map));
		}
		return ret;
	}

	@Override
	public int count(User o) {
		String sql = DBSimpleSQL.count(dbTable, this.condition(o));
		return this.getJdbcTemplate().queryForObject(sql, this.statement(o), Integer.class);
	}
	
}
